﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;
using Complaint.Models.Dtos.Validators;
using Complaint.Models.Dtos.JavascriptConverters;

namespace Complaint.Models.Dtos
{
    public partial class Contact : IEntity, IJsonConverter<Contact>, IShallowCopy<Contact>
    {
        #region Implementation of IEntity

        public bool IsValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(FirstName))
                yield return new RuleViolation("First name is required", "FirstName");

            if (String.IsNullOrEmpty(LastName))
            {
                yield return new RuleViolation("Last name is required", "LastName");
            }

            if (String.IsNullOrEmpty(JobTitle))
            {
                yield return new RuleViolation("Job title is required", "JobTitle");
            }

            if (String.IsNullOrEmpty(Email))
            {
                yield return new RuleViolation("Email is required", "Email");
            }

            if (!EmailValidator.IsValidEmail(Email))
            {
                yield return new RuleViolation("Email is not valid", "Email");
            }
        }

        #endregion

        #region Implementation of IJsonConverter<Contact>

        /// <summary>
        /// Serializes the current instance of the object into a Json formatted string.
        /// </summary>
        /// <returns>A Json representation of the current instance.</returns>
        public string ToJson()
        {
            var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new JavaScriptConverter[] { new ContactConverter() });

            return serializer.Serialize(this);
        }

        /// <summary>
        /// Creates a new instance of the current type from the passed Json string.
        /// </summary>
        /// <param name="json">The Json string representing the serialized instance.</param>
        /// <returns>A new instance of the current type.</returns>
        public Contact FromJson(string json)
        {
            var serializer = new JavaScriptSerializer();

            serializer.RegisterConverters(new JavaScriptConverter[] { new ContactConverter() });

            return serializer.Deserialize<Contact>(json);
        }

        #endregion

        #region Implementation of IShallowCopy<Contact>

        /// <summary>
        /// Shallow copies itself into the passed object.
        /// </summary>
        /// <param name="o">The object to copy into.</param>
        public void Copy(ref Contact o)
        {
            o.Title = Title;
            o.FirstName = FirstName;
            o.LastName = LastName;
            o.JobTitle = JobTitle;
            o.Email = Email;
        }

        #endregion
    }
}
